const peer = require('./peer-control')
console.log('in app.js')

peer.on('add-stream', stream => {
    play(stream)
})

const video = document.getElementById('screen-video')

function play(stream) {
    const video = document.getElementById('screen-video')
    video.srcObject = stream
    video.onloadedmetadata = function () {
        video.play()
    }
}

window.onkeydown = function (e) {
    //data {keyCode, meta, alt, ctrl, shift}
    const data = {
        keyCode: e.keyCode,
        shift: e.shiftKey,
        meta: e.metaKey,
        ctrl: e.ctrlKey,
        alt: e.altKey
    }
    console.log('key:',data)
    peer.emit('robot', 'key', data)
}

window.onmouseup = function (e) {
    //data {clientX, clientY, screen:{width, height}, video:{width, height} }
    const data = {}
    data.clientX = e.clientX
    data.clientY = e.clientY
    data.video = {
        width: video.getBoundingClientRect().width,
        height: video.getBoundingClientRect().height,
    }
    console.log('mouse:',data)
    peer.emit('robot', 'mouse', data)

}

